package com.int_tech.messionLog.dao;

import com.int_tech.messionLog.entity.MessionLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/20.
 */
public interface MessionLogDao extends JpaRepository<MessionLog,Integer> {
    List<MessionLog> findMessionLogByMessionNumberOrderByCreateTimeDesc(String messionNumber);
    List<MessionLog> findMessionLogByMessionNumber(String messionNumber);

    List<MessionLog> findByMessionNumberLikeOrderByCreateTimeDesc(String s);

    @Query("select MAX(m.messionCount) from MessionLog m")
    String findMaxCode();


    @Query("select m from MessionLog m where m.messionNumber like CONCAT('%',:messionNumber,'%') and m.carNumber =:carNumber order by m.createTime desc ")
    List<MessionLog> findByMessionNumberLike(@Param("messionNumber")String messionNumber, @Param("carNumber")String carNumber);


    @Query("select m from MessionLog m where m.carNumber =:carNumber order by m.createTime desc")
    List<MessionLog> findMessionLogByCarNumberLast(String carNumber);

    List<MessionLog> findByCarNumber(String carNumber);


//    @Query("select m from MessionLog m group by m.carNumber")
    @Query(value = "select m from MessionLog m where m.createTime in (select max(b.createTime) from MessionLog b group by b.carNumber)")
    List<MessionLog> findLogGroupByCarNumber();

    List<MessionLog> findMessionLogByCarNumberOrderByCreateTimeDesc(String carNumber);

    List<MessionLog> findFirstByCarNumberOrderByCreateTimeDesc(String carNumber);

    MessionLog findFirstByCreateTimeAfterAndMessionNumberNot(Date createTime, String messionNumber);

    List<MessionLog> findMessionLogByMessionNumberAndDoorMessage(String messionNumber2, String a2);

    MessionLog findFirstByMessionNumber(String messionNumber);

    MessionLog findLastByMessionNumber(String messionNumber);

    MessionLog findFirstByMessionNumberOrderByCreateTimeDesc(String messionNumber);

    MessionLog findFirstByMessionNumberOrderByCreateTimeAsc(String messionNumber);

    List<MessionLog> findMessionLogByMessionNumberAndCarNumberOrderByCreateTimeDesc(String messionId, String carNumber);

    @Query("select m from MessionLog m where m.carNumber =?1 and m.createTime >=?2 order by m.createTime desc")
    List<MessionLog> findFirstByCarNumberAndCreateTimeOrderByCreateTimeDesc(String carNumber, Date sTime);

    List<MessionLog> findByCreateTimeBefore(Date onDate);

    Page<MessionLog> findByGpsIsFail(Pageable pageable,int i);

    Page<MessionLog> findByGpsIsFailOrderByCreateTimeDesc(Pageable pageable, int i);

//    Page<MessionLog> findByGpsIsFail(Pageable pageRequest, int i);
}
